package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.helloUIBinder.client.events.*;
import com.helloUIBinder.client.receivers.BlogPostReceiver;

public class AdminPageWidget extends Composite {

    private static AdminBlogPostEditWidgetUiBinder uiBinder = GWT.create(AdminBlogPostEditWidgetUiBinder.class);

    interface AdminBlogPostEditWidgetUiBinder extends UiBinder<Widget, AdminPageWidget> {
    }

    @UiField
    HTMLPanel adminContainer;

    private AdminBlogPostTableWidget adminBlogPostTableWidget;
    private AdminBlogPostEditWidget adminBlogPostEditWidget;
    private AdminBlogPostCreateWidget adminBlogPostCreateWidget;


    protected SimpleEventBus eventBus = new SimpleEventBus();


    public AdminPageWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        adminBlogPostTableWidget = new AdminBlogPostTableWidget(eventBus);
        adminContainer.add(adminBlogPostTableWidget);

        eventBus.addHandler(OnAdminBlogPostTableClickEvent.TYPE, new OnAdminBlogPostTableClickHandler() {
            @Override
            public void click(OnAdminBlogPostTableClickEvent event) {
                if (event.getCurrentAction().equals(OnAdminBlogPostTableClickEvent.EDIT)) {
                    adminContainer.remove(adminBlogPostTableWidget);
                    adminBlogPostEditWidget = new AdminBlogPostEditWidget(event.getClickedBlogPost(), eventBus);
                    adminContainer.add(adminBlogPostEditWidget);
                }
                if (event.getCurrentAction().equals(OnAdminBlogPostTableClickEvent.CREATE)) {
                }
                if (event.getCurrentAction().equals(OnAdminBlogPostTableClickEvent.DELETE)) {
                    BlogPostReceiver.removeBlogPost(getRemoveBlogPostCallback(), event.getClickedBlogPost());
                }
            }
        });

        eventBus.addHandler(OnAdminBlogPostAddClickEvent.TYPE, new OnAdminBlogPostAddClickHandler() {
            @Override
            public void click(OnAdminBlogPostAddClickEvent event) {
                adminContainer.remove(adminBlogPostTableWidget);
                adminBlogPostCreateWidget = new AdminBlogPostCreateWidget(eventBus);
                adminContainer.add(adminBlogPostCreateWidget);
            }
        });

        eventBus.addHandler(OnAdminBlogPostCreateResultEvent.TYPE, new OnAdminBlogPostCreateResultHandler() {
            @Override
            public void result(OnAdminBlogPostCreateResultEvent event) {
                if (event.getResult().equals(OnAdminBlogPostEditResultEvent.CANCEL)) {
                    adminContainer.remove(adminBlogPostCreateWidget);
                    adminContainer.add(adminBlogPostTableWidget);
                }
                if (event.getResult().equals(OnAdminBlogPostCreateResultEvent.CREATE)) {
                    BlogPostReceiver.putBlogPost(getCreateBlogPostCallback(), event.getResultBlogPost());
                }
            }
        });

        eventBus.addHandler(OnAdminBlogPostEditResultEvent.TYPE, new OnAdminBlogPostEditResultHandler() {
            @Override
            public void result(OnAdminBlogPostEditResultEvent event) {
                if (event.getResult().equals(OnAdminBlogPostEditResultEvent.CANCEL)) {
                    adminContainer.remove(adminBlogPostEditWidget);
                    adminContainer.add(adminBlogPostTableWidget);
                }
                if (event.getResult().equals(OnAdminBlogPostEditResultEvent.SUBMIT)) {
                    BlogPostReceiver.putBlogPost(getEditBlogPostCallback(), event.getResultBlogPost());
                }
            }
        });
    }

    private RequestCallback getEditBlogPostCallback() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                adminContainer.remove(adminBlogPostEditWidget);
                adminContainer.add(adminBlogPostTableWidget);
                adminBlogPostTableWidget.refreshTable();
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private RequestCallback getCreateBlogPostCallback() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                adminContainer.remove(adminBlogPostCreateWidget);
                adminContainer.add(adminBlogPostTableWidget);
                adminBlogPostTableWidget.refreshTable();
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private RequestCallback getRemoveBlogPostCallback() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
//                if(response.getText().equals("SUCCESS")){
                    adminBlogPostTableWidget.refreshTable();
//                }else{
//                    Window.Location.assign(GWT.getHostPageBaseURL() + "index.html");
//                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                int a = 12;
                int b = 12;
                int c = a + b;
            }
        };
    }

}